[% setvar title True Polymorphic Objects %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>True Polymorphic Objects</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
  Date: 25 Aug 2000
  Last-Modified: 16 Sep 2000
  Mailing List: <a href='mailto:perl6-language-objects@perl.org'>perl6-language-objects@perl.org</a>
  Number: 159
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Currently, using objects in numeric and string contexts is not very
useful or easy:</p>
<pre>   $r = new CGI;
   $z = $r + $x;      # oops
   print &quot;$r\n&quot;;      # double-oops</pre>
<p>You can use facilities such as <code>tie</code> to help fix this issue, but <code>tie</code>
is limited and slow. You can also <code>use overload</code>, but this is not
flexible enough for many applications since it applies to a package (and
not individual objects).</p>
<p>This RFC proposes the concept of <b>true polymorphic objects</b>, which are
objects that can morph into numbers, strings, booleans, and much more
on-demand. As such, objects can be freely passed around and manipulated
without having to care what they contain (or even that they're objects).</p>
<a name='NOTES ON FREEZE'></a><h1>NOTES ON FREEZE</h1>
<p>The only comments received were function additions. No real oppositions
were recorded. As such, the functions have been added and the RFC
frozen.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='Overview'></a><h1>Overview</h1>
<p>The top-level syntax remains the same. As such, transition to Perl 6 is
very smooth for most people, and in fact most users don't have to care
about any of the following details. To them, this script will &quot;just
work&quot;:</p>
<pre>   $y = Math-&gt;data(7); 
   $x = 3;
   $name = getname(&quot;Nate&quot;);
   if ( $x &lt; 5 ) {
      $y += $x;
      if ( ! $name ) {
         $name = &quot;The math whiz&quot;;
      }
   }
   print &quot;$name got $y&quot;;   # &quot;Nate got 10&quot;</pre>
<p>However, under the hood things might work drastically differently. In
fact, <code>$y</code> and <code>$name</code> might well be polymorphic objects:</p>
<pre>   $y = Math-&gt;data(7);          # $y = Math-&gt;data; $y-&gt;STORE(7)
   $x = 3;                      # $x = 3
   $name = getname(&quot;Nate&quot;);     # $name = getname; $name-&gt;STORE(&quot;Nate&quot;)
   if ( $x &lt; 5 ) {              # $x &lt; 5
      $y += $x;                 # $y-&gt;STORE($y-&gt;PLUS($x))
      if ( ! $name ) {          # $name || $name-&gt;BOOLEAN
        $name = &quot;The math whiz&quot;;# $name-&gt;STORE(&quot;...&quot;)
      }
   }
   print &quot;$name got $y&quot;;        # $name-&gt;STRING , $y-&gt;STRING</pre>
<p>Here, <code>$y</code> and <code>$name</code> are objects, but we don't have to care. These
objects have a key property: <i>context sensitivity</i>. They have numerous
different methods which are each called only in specific instances. So,
being called in a numeric context calls <code>NUMBER</code>, whereas being called
in a string context would call <code>STRING</code>.</p>
<p>Plus, operators are overloadable as well. This means that we might
decide to overload <code>+</code> to become a Java-like concatenation operator on
our objects:</p>
<pre>   $string = $name + &quot;Wiger&quot;;   # $name-&gt;PLUS(&quot;Wiger&quot;)</pre>
<p>Yuck. :-) But it can be done, and that's pretty cool.</p>
<a name='Polymorphic Methods'></a><h2>Polymorphic Methods</h2>
<p>The following are the proposed methods for Perl 6 objects.  Note that
these methods are completely <i>optional</i> for a class to define. If they
are not defined, the object would retain its current behavior. The hooks
are in Perl if you want them, otherwise they don't get in the way.</p>
<p>Note that <code>STRING</code>, <code>NUMBER</code>, and <code>BOOLEAN</code> are specialized forms of
<code>FETCH</code>. If you define them, they are used instead of <code>FETCH</code> in the
given context, otherwise <code>FETCH</code> is used. Also note that the operators,
when overloaded, behave similarly to 'use overload', but on an
<i>object by object basis</i>, rather than package-wide.</p>
<p>Alternative names are listed in parens.</p>
<pre>   Data Conversion and Access
   -------------------------------------------------
   STRING           Called in a string context
   NUMBER           Called in a numeric context
   BOOLEAN          Called in a boolean context


   Operator Overloading
   -------------------------------------------------
   PLUS     (ADD)   Called in + context
   MINUS    (SUB)   Called in - context
   TIMES    (MUL)   Called in * context
   DIVIDE   (DIV)   Called in / context
   MODULUS  (MOD)   Called in % context
   CONCAT           Called in . context

   NUMCMP           Called in &lt;=&gt; context 
   NUMEQ            Called in == context
   NUMNE            Called in != context
   NUMLT            Called in &lt;  context
   NUMGT            Called in &gt;  context
   NUMLE            Called in &lt;= context
   NUMGE            Called in &gt;= context

   STRCMP           Called in cmp context
   STREQ            Called in eq context
   STRNE            Called in ne context
   STRLT            Called in lt context
   STRGT            Called in gt context
   STRLE            Called in le context
   STRGE            Called in ge context

   BITAND           Called in &amp; context
   BITOR            Called in | context
   BITXOR           Called in ^ context
   BITNOT           Called in ~ context

   LOGNOT           Called in ! context
   LOGHIGHAND       Called in &amp;&amp; context
   LOGHIGHOR        Called in || context
   LOGLOWAND        Called in and context
   LOGLOWOR         Called in or context
   LOGIFELSE        Called in ?: context


   Operator Fallbacks (like 'use overload')
   -------------------------------------------------
   OVERLOAD         Like an AUTOLOAD for the above,
                    perhaps NOMETHOD is a better name


   Assignment and Existence
   -------------------------------------------------
   CREATE           Called in variable creation (?)
   BUILD            Called in object blessing
   REBUILD          Called in object reblessing
   STORE            Called in an lvalue = context
   FETCH            Called in an rvalue = context
   DESTROY          Called in object destruction</pre>
<p>There are undoubtedly other functions that should be added. However, the
purpose of this RFC is to propose an idea and a basic interface. [1]</p>
<p>Now, whether the word <code>TIMES</code> is better than <code>MUL</code> is debateable. I
was trying to get the English verb context right, sticking with words:</p>
<pre>   $x * $y     # $x-&gt;TIMES($y)</pre>
<p>Which I think makes the function names intuitive and easy to remember. I
left out any potential _'s because these are a pain to type, and no
other Perl builtins have them.</p>
<p>The <code>CREATE</code> method may or may not serve to be useful; there is some
debate about what it could do since the true creation of an object
occurs on blessing, not on variable declaration. With embedded objects
it could well serve to prepare the object, optimizing storage space and
tinkering with internals. However, without embedded objects it is
probably of dubious usefulness. For more details and discussion on this
particular issue, please see <b>RFC 189: Hierarchical calls to
initializers and destructors</b>.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Fundamental hooks would have to be put into Perl in order to look for
these subs for the corresponding operations. Since it looks like
-internals will probably put vtable stuff in SV from the get-go, this
could potentially not be that tricky. At compile-time, Perl could simply
look for these subs in a class definition and create the proper hooks
for overloading, similar to how 'use overload' works now but at a lower
level.</p>
<p>Note that the <code>OVERLOAD</code> method serves a simliar purpose to <code>AUTOLOAD</code>
and the <code>nomethod</code> tag in overloading. If it exists, then it is used to
fallback for non-overloaded operations. Otherwise, the overloading
behavior is automatically determined by Perl. Perl should make these
decisions in <code>UNIVERSAL::OVERLOAD</code>, so that the user can defer to it in
their own <code>OVERLOAD</code> method if need be.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>This introduces new functionality, however p52p6 would have to catch any
subs defined with the names listed above and warn the user that this sub
name is now reserved for Perl.</p>
<p>Worth consideration with the proliferation of ALLCAPS methods and
variables is whether a warning like this:</p>
<pre>   ALLCAPS sub name may conflict with future reserved word at line 3.</pre>
<p>Might not be worth spitting out if a user defines a sub in ALLCAPS that
is not a recognized special Perl sub. This RFC does <i>not</i> take a stand
on this issue but rather provides it for consideration.</p>
<a name='NOTES'></a><h1>NOTES</h1>
<p>[1] Abiding by the KISS property (Keep It Simple, Stupid)</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 189: Hierarchical calls to initializers and destructors</p>
<p>RFC 49: Objects should have builtin stringifying STRING method</p>
<p>RFC 73: All Perl core functions should return objects</p>
<p>Perl's existing <code>use overload</code> and <code>tie</code></p>
<p><a href='http://www.mail-archive.com/perl6-language-objects@perl.org/msg00142.html' target='_blank'>www.mail-archive.com</a></p>
<a name='ACKNOWLEDGEMENTS'></a><h1>ACKNOWLEDGEMENTS</h1>
<p>Thanks to brian d foy and Damian Conway for their input</p>
<p>Thanks to Uri Guttman for suggesting CREATE on a different topic</p>
<p>Thanks to David Nicol for his suggestion of BOOLEAN</p>
</div>
